//
//  UIButton+MyButton.m
//  Qihuo
//
//  Created by Vencoo on 13-10-18.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "UIButton+MyButton.h"

@implementation UIButton (MyButton)
- (void)addImage:(UIImage*)image title:(NSString*)title
{
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    UILabel *label = [UILabel new];
    label.text = title;
    label.font = [UIFont systemFontOfSize:25];
    CGSize labSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    imageView.frame = CGRectMake((self.frame.size.width - image.size.width)/2, (self.frame.size.height/4*3 - image.size.height)/2, image.size.width, image.size.height);
    label.frame = CGRectMake((self.frame.size.width - labSize.width)/2, self.frame.size.height*3/4+(self.frame.size.height/4 - labSize.height)/2 - 8, labSize.width, labSize.height);
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    [self addSubview:imageView];
    [self addSubview:label];
    
}

-(void)addFirstImage:(UIImage *)image1 secondImage:(UIImage *)image2 forState:(UIControlState)state
{
    UIImageView *image2View = [[UIImageView alloc]initWithImage:image2];
    [self setImage:image1 forState:UIControlStateNormal];
    image2View.frame = CGRectMake(image1.size.width - image2.size.width, 0, image2.size.width, image2.size.height);
    image2View.userInteractionEnabled = YES;
    [self addSubview:image2View];
}

-(void)addFirstLine:(NSString *)string
{
    UILabel *label = [UILabel new];
    label.text = string;
    label.font = [UIFont systemFontOfSize:36];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    CGSize size;
    if ([string rangeOfString:@"+"].length > 0) {
        size = [[NSString stringWithFormat:@"啊啊+啊啊"]sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        label.frame = CGRectMake((self.frame.size.width - size.width)/2, (self.frame.size.height - size.height*2)/3, size.width, size.height*2+(self.frame.size.height - size.height*2)/3);
    }else if (string.length<5){
        size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        label.frame = CGRectMake((self.frame.size.width - size.width)/2, (self.frame.size.height - size.height)/2, size.width, size.height);
    }else if (string.length<9||[string rangeOfString:@"C C"].length > 0){
        size = [[NSString stringWithFormat:@"啊啊啊啊"]sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        label.frame = CGRectMake((self.frame.size.width - size.width)/2, (self.frame.size.height - size.height*2)/3, size.width, size.height*2+(self.frame.size.height - size.height*2)/3);
    }else if (string.length<11){
        size = [[NSString stringWithFormat:@"啊啊啊啊啊"]sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        label.frame = CGRectMake((self.frame.size.width - size.width)/2, (self.frame.size.height - size.height*2)/3, size.width, size.height*2+(self.frame.size.height - size.height*2)/3);
    }else{
        size = [[NSString stringWithFormat:@"啊啊啊啊啊"]sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
        label.frame = CGRectMake((self.frame.size.width - size.width)/2, (self.frame.size.height - size.height*3)/3, size.width, size.height*3+(self.frame.size.height - size.height*3)/3);
    }
        label.textAlignment = 1;
        [label setNumberOfLines:0];//自动换行
        [self addSubview:label];
    
}
@end
